package com.smiier.skin;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.o.app.event.listener.OBitmapLoadCallBack;
import cn.o.app.json.JsonUtil;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.skinapp.R;

import com.pifuke.adapter.SampleUserAdapter;
import com.pifuke.adapter.TreatmentCommentsAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.extra.TreatmentProjectItemExtra;
import com.smiier.skin.net.DoctorTreatmentProjectListTask.DoctorTreatmentProjectItem;
import com.smiier.skin.net.TreatmentProjectDetailTask;
import com.smiier.skin.net.TreatmentProjectDetailTask.TreatmentProjectDetailRequest;
import com.smiier.skin.net.TreatmentProjectDetailTask.TreatmentProjectDetailResponse;
import com.smiier.skin.net.entity.Hospital.HospitalItem;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.net.entity.UserWrapper;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.ui.ListViewForScrollView;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.util.ShareUtil;

public class TreatmentDetailActivity extends BasicActivity implements
		OnClickListener {

	TextView mTreatmentName;
	TextView mTreatmentHospital;
	TextView mTreatmentPrice;
	TextView mTreatmentPriceOrign;
	WebView mTreatmentDesc;
	TextView mTreatmentOther;
	ListView mDoctorList;
	ListViewForScrollView mCommentList;
	TreatmentCommentsAdapter mCommentAdapter;
	SampleUserAdapter mDoctorAdapter;
	LinearLayout mCommentParent;
	TextView mTreatmentReserve;
	TreatmentProjectItemExtra extra = new TreatmentProjectItemExtra();
	DoctorTreatmentProjectItem item;
	RelativeLayout mMoreComment;
	TextView mAddress;
	String mTel;
	TextView mCall;
	ImageView mImage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_treatment_detail);
		init();
		setNavTitle("治疗详情");
		setNavRightBtnIcon(R.drawable.btn_share);

		Intent i = getIntent();
		extra.getFrom(i);
		item = extra.getUser();
		mImage = findViewById(R.id.treatment_detail_img, ImageView.class);
		mTreatmentName = findViewById(R.id.treatment_detail_name,
				TextView.class);
		mTreatmentHospital = findViewById(R.id.treatment_detail_hospital,
				TextView.class);
		mTreatmentReserve = findViewById(R.id.treatment_reserver,
				TextView.class);
		mTreatmentPrice = findViewById(R.id.treatment_price, TextView.class);
		mTreatmentPriceOrign = findViewById(R.id.treatment_price_orgin,
				TextView.class);
		mTreatmentDesc = findViewById(R.id.treatment_desc, WebView.class);
		mTreatmentOther = findViewById(R.id.treatment_desc_other,
				TextView.class);

		mDoctorList = findViewById(R.id.doctor_list, ListView.class);
		mCommentList = findViewById(R.id.comment_list,
				ListViewForScrollView.class);
		mDoctorAdapter = new SampleUserAdapter();
		mCommentAdapter = new TreatmentCommentsAdapter();
		mCommentList.setAdapter(mCommentAdapter);
		mDoctorList.setAdapter(mDoctorAdapter);
		mCommentParent = findViewById(R.id.comments_parent, LinearLayout.class);
		mMoreComment = findViewById(R.id.load_more_comments,
				RelativeLayout.class);
		mCall = findViewById(R.id.treatment_tel, TextView.class);
		mCall.setOnClickListener(this);
		mAddress = findViewById(R.id.hospital_address, TextView.class);
		mTreatmentReserve.setOnClickListener(this);
		mMoreComment.setOnClickListener(this);
		mDoctorList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				User user = mDoctorAdapter.getDataProvider().get(position).User;
				Intent i = new Intent(TreatmentDetailActivity.this,
						DoctorDetailActivity.class);
				try {
					i.putExtra(Constant.IDENTITY_KEY, JsonUtil.convert(user));

					startActivity(i);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		WebSettings webSettings = mTreatmentDesc.getSettings();
		// webSettings.setJavaScriptEnabled(true);
		// webSettings.setUseWideViewPort(true);
		// webSettings.setLoadWithOverviewMode(true);
		webSettings.setJavaScriptEnabled(true);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		webSettings.setAllowFileAccess(true);// 设置允许访问文件数据
		webSettings.setSupportZoom(true);
		webSettings.setBuiltInZoomControls(true);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		webSettings.setDomStorageEnabled(true);
		webSettings.setDatabaseEnabled(true);

		mTreatmentDesc.setWebChromeClient(new WebChromeClient());
	}

	@Override
	protected void onStart() {
		super.onStart();
		initbasic();
	}

	@Override
	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.treatment_reserver) {
			Intent i = new Intent(this, TreatmentReserveActivity.class);
			if (extra.putTo(i)) {
				startActivity(i);
			}
		} else if (id == R.id.load_more_comments) {
			Intent i = new Intent(this, CommentListActivity.class);
			if (extra.putTo(i)) {
				startActivity(i);
			}
		} else if (id == R.id.treatment_tel) {
			Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ mTel));
			startActivity(intent);
		} else if (id == R.id.text_right) {
			// http://123.57.73.61/Client/TreatmentProject/hospitalHome/hospital/
			ShareUtil.share(this, GlobalSettings.SERVER_URL_TREATMENT
					+ "WeChat/MenuData/projectInfo/projectid/" + item.id,
					item.name, "");
			// ShareUtil.share(this, GlobalSettings.SERVER_URL_TREATMENT
			// + "Client/TreatmentProject/hospitalHome/hospital/"
			// + item.hospital, item.hospital, "");
		}
	}

	private void initbasic() {
		mTreatmentName.setText(item.name);
		mTreatmentHospital.setText(item.hospital);
		if (item.original_price <= 0) {
			mTreatmentPriceOrign.setText("具体价格请咨询医院");
			mTreatmentPrice.setText("");
		} else {
			mTreatmentPriceOrign.getPaint().setFlags(
					Paint.STRIKE_THRU_TEXT_FLAG);
			mTreatmentPrice.setText("￥" + item.discount_price + "");
			mTreatmentPriceOrign.setText("￥" + item.original_price + "");
		}
		mBitmapUtils.display(mImage, item.url, new OBitmapLoadCallBack<View>());
		boolean hasHos = false;
		for (HospitalItem hos : GlobalSettings.mHospitalItems) {
			if (item.hospital.equals(hos.Name)
					|| item.hospital.equals(hos.FullName)
					|| item.hospital.contains(hos.Name)
					|| item.hospital.contains(hos.FullName)
					|| hos.Name.contains(item.hospital)
					|| hos.FullName.contains(item.hospital)) {
				hasHos = true;
				mAddress.setText(hos.Address);
				String[] array = hos.Tel.split(",");
				if (array.length > 0) {
					int index = array[0].indexOf("(");
					mTel = array[0].substring(0, index).replace("-", "");
				}
				break;
			}
		}
		if (!hasHos) {
			mAddress.setVisibility(View.GONE);
		}
		/*
		 * TreatmentProjectDoctorListTask task = new
		 * TreatmentProjectDoctorListTask(); TreatmentProjectDoctorListRequest
		 * req = new TreatmentProjectDoctorListRequest(); req.hospital =
		 * item.hospital; task.setRequest(req); task.addListener(new
		 * NetTaskListener<TreatmentProjectDoctorListRequest,
		 * TreatmentProjectDoctorListResponse>() {
		 * 
		 * @Override public void onComplete(
		 * INetTask<TreatmentProjectDoctorListRequest,
		 * TreatmentProjectDoctorListResponse> task,
		 * TreatmentProjectDoctorListResponse response) { if (response != null
		 * && response.ResultCode == 200) { response.Res.Users; ArrayList<User>
		 * users = new ArrayList<User>(); for (UserWrapper user : Users) {
		 * users.add(user.User); } if (users.size() > 0) {
		 * 
		 * } } }
		 * 
		 * @Override public void onException(
		 * INetTask<TreatmentProjectDoctorListRequest,
		 * TreatmentProjectDoctorListResponse> task, Exception e) {
		 * e.printStackTrace(); } }); add(task);
		 */

		TreatmentProjectDetailTask dtask = new TreatmentProjectDetailTask();
		TreatmentProjectDetailRequest dreq = new TreatmentProjectDetailRequest();
		dreq.id = item.id + "";
		dtask.setRequest(dreq);
		dtask.addListener(new NetTaskListener<TreatmentProjectDetailTask.TreatmentProjectDetailRequest, TreatmentProjectDetailTask.TreatmentProjectDetailResponse>() {
			@Override
			public void onComplete(
					INetTask<TreatmentProjectDetailRequest, TreatmentProjectDetailResponse> task,
					TreatmentProjectDetailResponse response) {

				if (response != null) {
					// mTreatmentDesc.setText(response.basic_description);

					if (response.basic_description != null
							&& !response.basic_description.trim().equals("")) {
						mTreatmentDesc.loadUrl(response.basic_description);
					}
					mTreatmentOther.setText(response.other_description);
					if (response.comments != null
							&& response.comments.size() > 0) {
						mCommentParent.setVisibility(View.VISIBLE);
						mCommentAdapter.setDataProvider(response.comments);
						mCommentAdapter.notifyDataSetChanged();
					}

					if (response.doctor_info != null
							&& response.doctor_info.size() > 0) {
						mDoctorList.setVisibility(View.VISIBLE);
						ArrayList<UserWrapper> Users = new ArrayList<UserWrapper>();
						for (User u : response.doctor_info) {
							UserWrapper wrapper = new UserWrapper();
							wrapper.Uid = u.Uid;
							wrapper.User = u;
							Users.add(wrapper);
						}
						mDoctorAdapter.setDataProvider(Users);
						mDoctorAdapter.notifyDataSetChanged();
					}
				}
			}

			@Override
			public void onException(
					INetTask<TreatmentProjectDetailRequest, TreatmentProjectDetailResponse> task,
					Exception e) {

				e.printStackTrace();

			}

		});
		add(dtask);
	}

	/*
	 * @Override public void onResume() { super.onResume();
	 * MobclickAgent.onResume(getContext()); }
	 * 
	 * @Override public void onPause() { super.onPause();
	 * MobclickAgent.onPause(getContext()); }
	 */
}
